package top.igotcha.educenter.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.igotcha.educenter.entity.UcenterMember;
import top.igotcha.educenter.entity.vo.RegisterVo;
import top.igotcha.educenter.service.UcenterMemberService;
import top.igotcha.commonutils.ordervo.UcenterMemberOrder;
import top.igotcha.commonutils.utils.JwtUtils;
import top.igotcha.commonutils.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {
    @Autowired
    UcenterMemberService memberService;

    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return R.OK().data("token",token);
    }

    @PostMapping("register")
    public R register(@RequestBody RegisterVo vo){
        memberService.register(vo);
        return R.OK();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        if (!JwtUtils.checkToken(request)){
            return R.ERROR().message("token非法");
        }
        String memberId  = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        System.out.println(member);
        return R.OK().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @ApiOperation("根据用户id获取用户信息,用于远程调用")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    @ApiOperation("查询某一天注册人数")
    @PostMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.OK().data("countRegister",count);
    }

}

